/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.rotaryTableRecord.rest;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.api.ApiResult;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.rotaryTableRecord.domain.RotaryTableRecord;
import co.yixiang.modules.rotaryTableRecord.service.RotaryTableRecordService;
import co.yixiang.modules.rotaryTableRecord.service.dto.RotaryTableRecordDto;
import co.yixiang.modules.rotaryTableRecord.service.dto.RotaryTableRecordQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
* @author hupeng
* @date 2020-12-23
*/
@AllArgsConstructor
@Api(tags = "RotaryTableRecord管理")
@RestController
@RequestMapping("/api/rotaryTableRecord")
public class RotaryTableRecordController {

    private final RotaryTableRecordService rotaryTableRecordService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','rotaryTableRecord:list')")
    public void download(HttpServletResponse response, RotaryTableRecordQueryCriteria criteria) throws IOException {
        rotaryTableRecordService.download(generator.convert(rotaryTableRecordService.queryAll(criteria), RotaryTableRecordDto.class), response);
    }

    @GetMapping
    @Log("查询RotaryTableRecord")
    @ApiOperation("查询RotaryTableRecord")
    @PreAuthorize("@el.check('admin','rotaryTableRecord:list')")
    public ResponseEntity<Object> getRotaryTableRecords(RotaryTableRecordQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(rotaryTableRecordService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增RotaryTableRecord")
    @ApiOperation("新增RotaryTableRecord")
    @PreAuthorize("@el.check('admin','rotaryTableRecord:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody RotaryTableRecord resources){
        return new ResponseEntity<>(rotaryTableRecordService.save(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改RotaryTableRecord")
    @ApiOperation("修改RotaryTableRecord")
    @PreAuthorize("@el.check('admin','rotaryTableRecord:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody RotaryTableRecord resources){
        rotaryTableRecordService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除RotaryTableRecord")
    @ApiOperation("删除RotaryTableRecord")
    @PreAuthorize("@el.check('admin','rotaryTableRecord:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        Arrays.asList(ids).forEach(id->{
            rotaryTableRecordService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     *录入记录
     */
    @GetMapping("/updateStatus")
    @ApiOperation(value = "录入记录", notes = "录入记录")
    public ResponseEntity<Object>  updateStatus(@Validated @RequestParam String id){
        if(ObjectUtil.isNotEmpty(id)){
            RotaryTableRecord rotaryTableRecord =rotaryTableRecordService.getById(id);
            rotaryTableRecord.setStatus(1);
            rotaryTableRecordService.saveOrUpdate(rotaryTableRecord);
            return new ResponseEntity<>(HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
